﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace BluetoothMachine.ExpandHelper
{
    public class MotorMovements
    {
        public static void SetXSpeed(double speed)
        {
            ConfigurationAxisMovement(LTDMC.CardID[0], 0, 1000, speed, 0.200, 0.200, 1000, 0.01);
            ConfigurationAxisMovement(LTDMC.CardID[0], 3, 1000, speed, 0.200, 0.200, 1000, 0.01);
            ConfigurationAxisMovement(LTDMC.CardID[0], 6, 1000, speed, 0.200, 0.200, 1000, 0.01);
            ConfigurationAxisMovement(LTDMC.CardID[1], 1, 1000, speed, 0.200, 0.200, 1000, 0.01);
            ConfigurationAxisMovement(LTDMC.CardID[1], 4, 1000, speed, 0.200, 0.200, 1000, 0.01);
        }

        public static void SetYSpeed(double speed)
        {
            ConfigurationAxisMovement(LTDMC.CardID[0], 1, 1000, speed, 0.200, 0.200, 1000, 0.01);
            ConfigurationAxisMovement(LTDMC.CardID[0], 4, 1000, speed, 0.200, 0.200, 1000, 0.01);
            ConfigurationAxisMovement(LTDMC.CardID[0], 7, 1000, speed, 0.200, 0.200, 1000, 0.01);
            ConfigurationAxisMovement(LTDMC.CardID[1], 2, 1000, speed, 0.100, 0.200, 1000, 0.01);
            ConfigurationAxisMovement(LTDMC.CardID[1], 5, 1000, speed, 0.100, 0.200, 1000, 0.01);
        }

        public static void SetZSpeed(double speed)
        {
            ConfigurationAxisMovement(LTDMC.CardID[0], 2, 1000, speed, 0.200, 0.200, 1000, 0.01);
            ConfigurationAxisMovement(LTDMC.CardID[0], 5, 1000, speed, 0.200, 0.200, 1000, 0.01);
            ConfigurationAxisMovement(LTDMC.CardID[1], 0, 1000, speed, 0.200, 0.200, 1000, 0.01);
            ConfigurationAxisMovement(LTDMC.CardID[1], 3, 1000, speed, 0.200, 0.200, 1000, 0.01);
            ConfigurationAxisMovement(LTDMC.CardID[1], 6, 1000, speed, 0.200, 0.200, 1000, 0.01);
        }

        private static void ConfigurationAxisMovement(ushort cardid, ushort axis, double dStartVel, double dMaxVel, double dTacc, double dTdec, double dStopVel, double dS_para)
        {
            LTDMC.dmc_set_profile(cardid, axis, dStartVel, dMaxVel, dTacc, dTdec, dStopVel);//设置速度参数
            //LTDMC.dmc_set_s_profile(cardid, axis, 0, dS_para);//设置S段速度参数
            //LTDMC.dmc_set_dec_stop_time(cardid, axis, dTdec); //设置减速停止时间
        }
        public static void InitLTDC()
        {
            short num = LTDMC.dmc_board_init();//获取卡数量
            if (num <= 0 || num > 8)
            {
                MessageBox.Show("初始卡失败!\r\n停-止-运-行", "出 错");
                return;
            }
            ushort _num = 0;
            ushort[] cardids = new ushort[8];
            uint[] cardtypes = new uint[8];
            short res = LTDMC.dmc_get_CardInfList(ref _num, cardtypes, cardids);
            if (res != 0)
            {
                MessageBox.Show("获取卡信息失败!", "出错");
                Application.Current.Shutdown();
            }
            LTDMC.CardID = new ushort[] { cardids[0], cardids[1] };
        }
        public static void BackOrigin()
        {
            #region 设置X轴回原点方式
            LTDMC.dmc_set_homemode(LTDMC.CardID[0], 0, 0, 1, 2, 0);
            LTDMC.dmc_set_homemode(LTDMC.CardID[0], 3, 0, 1, 2, 0);

            LTDMC.dmc_set_homemode(LTDMC.CardID[0], 6, 0, 1, 2, 0);
            LTDMC.dmc_set_homemode(LTDMC.CardID[1], 1, 0, 1, 2, 0);
            LTDMC.dmc_set_homemode(LTDMC.CardID[1], 4, 0, 1, 2, 0);

            #endregion

            #region 设置Y轴回原点方式
            LTDMC.dmc_set_homemode(LTDMC.CardID[0], 1, 0, 1, 2, 0);
            LTDMC.dmc_set_homemode(LTDMC.CardID[0], 4, 0, 1, 2, 0);

            LTDMC.dmc_set_homemode(LTDMC.CardID[0], 7, 0, 1, 2, 0);
            LTDMC.dmc_set_homemode(LTDMC.CardID[1], 2, 0, 1, 2, 0);
            LTDMC.dmc_set_homemode(LTDMC.CardID[1], 5, 0, 1, 2, 0);

            #endregion

            #region 设置Z轴回原点方式 
            LTDMC.dmc_set_homemode(LTDMC.CardID[0], 2, 0, 1, 0, 0);
            LTDMC.dmc_set_homemode(LTDMC.CardID[0], 5, 0, 1, 0, 0);

            LTDMC.dmc_set_homemode(LTDMC.CardID[1], 0, 0, 1, 0, 0);
            LTDMC.dmc_set_homemode(LTDMC.CardID[1], 3, 0, 1, 0, 0);
            LTDMC.dmc_set_homemode(LTDMC.CardID[1], 6, 0, 1, 0, 0);
            #endregion
            for (int i = 0; i < 16; i++)
            {
                LTDMC.dmc_set_home_el_return(LTDMC.CardID[i > 7 ? 1 : 0], (ushort)(i % 8), 1);
            }
        }
        /// <summary>
        /// XY轴运动
        /// </summary>
        /// <param name="cardid">卡号</param>
        /// <param name="axis">轴号</param>
        /// <param name="dDist">运动距离</param>
        public static void XYMove(ushort cardid, ushort[] axis, double[] dDist)
        {
            //log4net.LogManager.GetLogger("message").Info($"运动距离 X：{dDist[0]} Y：{dDist[1]}");
            if (dDist[0] != 0) ShaftOperation(cardid, axis[0], (int)(dDist[0] / 0.02));
            if (dDist[1] != 0) ShaftOperation(cardid, axis[1], (int)(dDist[1] / 0.02));
        }

        /// <summary>
        /// 单轴运行
        /// </summary>
        /// <param name="cardid">卡号</param>
        /// <param name="axis">轴号</param>
        /// <param name="dDist">脉冲数</param>
        /// <returns></returns>
        public static void ShaftOperation(ushort cardid, ushort axis, int dDist)
        {
            LTDMC.dmc_pmove(cardid, axis, dDist, 1);//定长运动  0 相对运动 1 绝对运动
            WaitAxisMovement(cardid, axis);
        }

        /// <summary>
        /// 回原点
        /// </summary>
        /// <param name="cardid">卡号</param>
        /// <param name="axis"></param>
        /// <returns></returns>
        public static void HomeMove(ushort cardid, ushort axis)
        {
            LTDMC.dmc_home_move(cardid, axis);
            WaitAxisMovement(cardid, axis);
        }

        /// <summary>
        /// 连续运动
        /// </summary>
        /// <param name="cardid">卡号</param>
        /// <param name="axis">轴号</param>
        /// <param name="dir">运动方向，0：负方向，1：正方向</param>
        public static void DmcVmove(ushort cardid, ushort axis, ushort dir = 1)
        {
            LTDMC.dmc_vmove(cardid, axis, dir);
            WaitAxisMovement(cardid, axis);
        }

        /// <summary>
        /// 等待轴运动完成
        /// </summary>
        /// <param name="axis">轴号【0-7】</param>
        /// <returns></returns>
        public static void WaitAxisMovement(ushort cardid, ushort axis)
        {
            while (LTDMC.dmc_check_done(cardid, axis) == 0)
            {
                Thread.Sleep(5);
            }
        }

        /// <summary>
        /// 气缸运动
        /// </summary>
        /// <param name="cardid">卡号</param>
        /// <param name="biton">IO点</param>
        /// <param name="time">延时</param>
        public static void CylinderMovement(ushort cardid, ushort[] biton, long time)
        {
            LTDMC.dmc_write_outbit(cardid, biton[0], 0);
            LTDMC.dmc_write_outbit(cardid, biton[1], 0);
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Restart();
            while (stopwatch.ElapsedMilliseconds < time)
            {
                Thread.Sleep(5);
            }
            LTDMC.dmc_write_outbit(cardid, biton[0], 1);
            LTDMC.dmc_write_outbit(cardid, biton[1], 1);
        }
    }
}
